//
//  CoursedInstructorVc.m
//  Courses
//
//  Created by xxx on 15/1/13.
//  Copyright (c) 2015年 wtf. All rights reserved.
//

#import "CoursedInstructorVc.h"

#import "Teacher.h"

#import "InstructorPublicTableVc.h"
#import "InstructorAdvancedTableVc.h"

#import "UIImageView+WebCache.h"
#import "UILabel+AutoSize.h"
#import "PxhLabel.h"
@implementation CoursedInstructorVc
{
    Teacher *_teacher;
    NSArray *_publicCourses;
    NSArray *_advancedCourses;
    
    PxhLabel *_lblDescriptionLong;
    UITableView *_publicTableV;
    UITableView *_advancedTableV;
    
    InstructorPublicTableVc *_publicVc;
    InstructorAdvancedTableVc *_advancedVc;
}

static const CGFloat kHeaderHeight = 93;
static const CGFloat kSegmentHeight = 30;
static const CGFloat kSegmentMargin = 10;

- (void)viewDidLoad
{
    [super viewDidLoad];
    //提示网络状态
    [self showNetStatus];
    //
    self.title = self.teacher.TeacherName;
    
    [self addSegment];
    
    [self addHeaderView];
    [self addDescriptionView];
    [self addPublicTableView];
    [self addAdvancedTableView];
    
    [self loadTeacherDescriptionLong];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)addHeaderView
{
    UIView *headerView = [[UIView alloc] initWithFrame:AutoRectMake(0, 0, DEFAULT_WIDTH, kHeaderHeight, WTFAutoLayoutTypeBar)];
    
    const CGFloat kPaddingX = 20;
    const CGFloat kPaddingY = 20;
    const CGFloat kAvatarDiameter = 60;
    
    UIImageView *imgViewAvatar = [[UIImageView alloc] initWithFrame:AutoRectMake(kPaddingX, kPaddingY, kAvatarDiameter, kAvatarDiameter, WTFAutoLayoutTypeRaw)];
    [imgViewAvatar sd_setImageWithURL:[NSURL URLWithString:_teacher.TeacherHeadPhotoUrl]];
    imgViewAvatar.clipsToBounds = YES;
    imgViewAvatar.layer.cornerRadius = kAvatarDiameter / 2;
    imgViewAvatar.contentMode = UIViewContentModeScaleAspectFill;
    [headerView addSubview:imgViewAvatar];
    
    CGFloat kTextLeft = kAvatarDiameter + kPaddingX * 2;
    CGFloat kTitleHeight = 20;
    UILabel *labelTeacherName = [[UILabel alloc] initWithFrame:AutoRectMake(kTextLeft, kPaddingY, 200, kTitleHeight, WTFAutoLayoutTypeRaw)];
    labelTeacherName.text = _teacher.TeacherName;
    [headerView addSubview:labelTeacherName];
    
    UILabel *labelTeacherDescriptionShort = [[UILabel alloc] initWithFrame:AutoRectMake(kTextLeft, kPaddingY + kTitleHeight, DEFAULT_WIDTH - kTextLeft - kPaddingX, kAvatarDiameter - kTitleHeight, WTFAutoLayoutTypeRaw)];
    NSLog(@"teacher description short: %@", _teacher.TeacherdescShort);
    labelTeacherDescriptionShort.text = _teacher.TeacherdescShort;
    [headerView addSubview:labelTeacherDescriptionShort];
    
    [self.view addSubview:headerView];
}

- (void)addSegment
{
//    UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"介绍", @"公开课", @"高级课"]];
    UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"介绍", @"公开课"]];
    seg.selectedSegmentIndex = 0;
    seg.frame = AutoRectMake(20, kHeaderHeight, 280, kSegmentHeight, WTFAutoLayoutTypeBar);
    seg.tintColor = [WTFColors defaultBarColor];
    [seg addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:seg];
}

- (void)segmentAction:(UISegmentedControl *)seg
{

    NSLog(@"index: %d", seg.selectedSegmentIndex);
    _lblDescriptionLong.hidden = YES;
    _publicTableV.hidden = YES;
    _advancedTableV.hidden = YES;
    
    switch (seg.selectedSegmentIndex) {
        case 0:
            _lblDescriptionLong.hidden = NO;
            break;
            
        case 1:
            _publicTableV.hidden = NO;
            [_publicVc loadRequest];
            break;
            
        case 2:
            _advancedTableV.hidden = NO;
            [_advancedVc reloadRequest];
            break;
            
        default:
            break;
    }
}

- (CGRect)sharedFrame
{
    CGFloat y = RevFloat(kHeaderHeight + kSegmentHeight + kSegmentMargin - 10);
    return AutoRectMake(0, y, DEFAULT_WIDTH, DEFAULT_TABLE_VIEW_HEIGHT + DEFAULT_TABBAR_HEIGHT - y, WTFAutoLayoutTypePic);
}

- (void)addDescriptionView
{
    if (_lblDescriptionLong) return;
    //将backview做成可滑动的
    UIScrollView* scrollBackView = [[UIScrollView alloc] initWithFrame:AutoRectMake(0, [self sharedFrame].origin.y, [self sharedFrame].size.width, [self sharedFrame].size.height, WTFAutoLayoutTypeText)];
    scrollBackView.contentSize = CGSizeMake(DEFAULT_WIDTH, DEFAULT_HEIGHT * 1.5);
    scrollBackView.delegate = self;
    scrollBackView.backgroundColor = [UIColor clearColor];
    scrollBackView.showsVerticalScrollIndicator = NO;
    
    CGFloat paddingX = 20, paddingY = 15;
    _lblDescriptionLong = [[PxhLabel alloc] initWithFrame:AutoRectMake(paddingX, paddingY, scrollBackView.width - paddingX * 2, 3200, WTFAutoLayoutTypePic)];
    [_lblDescriptionLong sizeToFit];
    [scrollBackView addSubview:_lblDescriptionLong];
    [self.view addSubview:scrollBackView];
}

- (void)loadTeacherDescriptionLong
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        _teacher = [Teacher getTeacherDetailsById:_teacher.TeacherId];
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"description long: %@", _teacher.teacherDescLong);
            _lblDescriptionLong.text = _teacher.teacherDescLong;
            
            CGFloat prefferedWith = [self sharedFrame].size.width - 20 * 2;
            [_lblDescriptionLong autoSetSizeWithPreferedWidth:prefferedWith preferedHeight:25];
        });
    });
}

- (void)addPublicTableView
{
    if (_publicTableV) return;
    _publicTableV = [[UITableView alloc] initWithFrame:[self sharedFrame] style:UITableViewStylePlain];
    _publicTableV.tableFooterView = [[UIView alloc] init];
    _publicTableV.hidden = YES;
    _publicVc = [[InstructorPublicTableVc alloc] init];
    _publicVc.rootVc = self;
    _publicVc.tableView = _publicTableV;
    _publicVc.teacherId = _teacher.TeacherId;
    [self.view addSubview:_publicTableV];
}

- (void)addAdvancedTableView
{
    if (_advancedTableV) return;
    _advancedTableV = [[UITableView alloc] initWithFrame:[self sharedFrame] style:UITableViewStylePlain];
    _advancedTableV.tableFooterView = [[UIView alloc] init];
    _advancedTableV.hidden = YES;
    _advancedVc = [[InstructorAdvancedTableVc alloc] init];
    _advancedVc.rootVc = self;
    _advancedVc.teacherId = _teacher.TeacherId;
    _advancedVc.tableView = _advancedTableV;
    [self.view addSubview:_advancedTableV];
}

#pragma mark - http methods

@end
